package com.itheima.tliaswebmanagement.controller;

import com.itheima.tliaswebmanagement.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@Slf4j
public class UploadController {

//    private static final String UPLOAD_DIR = "D:/images/";

    @Autowired
    private AliyunOSSOperator aliyunOSSOperator;
    @PostMapping("/upload")
    public Result upload( MultipartFile file) throws Exception {
//       log.info("上传文件：{},{},{}",username,age,file);

        String url = aliyunOSSOperator.upload(file.getBytes(),file.getOriginalFilename());


      /*  if (!file.isEmpty()) {
            String originalFilename = file.getOriginalFilename();
            String extName = originalFilename.substring(originalFilename.lastIndexOf("."));
            String uniqueFileName = UUID.randomUUID().toString().replace("-", "") + extName;
            File targetFile = new File(UPLOAD_DIR + uniqueFileName);
//            file.transferTo(new File("D:/img" + file.getOriginalFilename()));
            //如果目标目录不存在，则创建它
            if (!targetFile.getParentFile().exists()) {
                targetFile.getParentFile().mkdirs();
            }
            // 保存文件
            file.transferTo(targetFile);
        }*/
        return Result.success(url);
    }
}
